<?php

namespace App\Http\Controllers;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ImageController extends Controller
{
    public function upload(Request $request)
    {
        $this->validate($request, [
            'image' => 'required|image',
        ]);

        $path = $request->file('image')->store('public/images');
        if (false === $path) {
            $response = [
                'message' => '上传失败，稍后再试。',
                'code'    => 40000,
            ];
        } else {
            $response = [
                'message' => '上传完成。',
                'code'    => 10000,
                'data' => [
                    'url'     => url(\Storage::url($path))
                ],
            ];
        }

        return JsonResponse::create($response);
    }
}
